import { getUserAuthority } from '@/unit/auth';
import { copyLinearArray } from '@/unit/loot'

// 遍历路由和权限
export const filterRouter = (router) => {
	// 权限路由
	let activityRouter = []
	activityRouter.splice(0)
	let list = copyLinearArray(router[3].children)
	activityRouter.push(...list)
	// 常量路由
	let constantRohtes = []
	constantRohtes.splice(0)
	let lists = copyLinearArray(router)
	constantRohtes.push(...lists)
	
	constantRohtes[3].children = []
	// 展示给用户的路由
	let arrRouter = []
	arrRouter = constantRohtes
	arrRouter[3].children = []
	let arr = getUserAuthority().split('@')
	let num = 0
	activityRouter.map((activityItem, index) => {
		if(index === 0){
			arr.map(arrItem => {
				if(activityItem.name === arrItem){
					arrRouter[3].children.push(activityItem)
				}
			})
		} else {
			activityItem.children.map(activityChildrenItem => {
				arr.map(arrItem => {
					if(activityChildrenItem.name === arrItem){
						if(num === 0){
							arrRouter[3].children.push(activityItem)
							arrRouter[3].children[arrRouter[3].children.length - 1].children = []
						}
						arrRouter[3].children[arrRouter[3].children.length - 1].children.push(activityChildrenItem)
						num++
					}
				})
			})
			num = 0
		}
	})
	return arrRouter
}